﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Threading;
using System.Globalization;

namespace Chart
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("zh-CN");
        }
        
        
        [WebMethod]
        public static string GetPartNoListByLineID(string lineID)
        {
            List<string> partNoList = MetrixDataBusiness.GetPartNoListByLineID(lineID);
            JavaScriptSerializer js = new JavaScriptSerializer();
            string json = js.Serialize(partNoList);
            return json;
            
        }

        [WebMethod]
        public static string GetLineList()
        {
            List<string> lineList = MetrixDataBusiness.GetLineList();
            JavaScriptSerializer js = new JavaScriptSerializer();
            string json = js.Serialize(lineList);
            return json;
        }

        [WebMethod]
        public static string GetTestPlanItemListByLineIDPartNoID(string lineID, string partNoID)
        {
            List<TestPlanItem> testPlanList = MetrixDataBusiness.GetTestPlanIDList(lineID, partNoID);
            JavaScriptSerializer js = new JavaScriptSerializer();
            string json = js.Serialize(testPlanList);
            return json;
        }

        [WebMethod]
        public static string GetChartData(string lineID, string partNoID, string testPlanID, string functionalTestID, string dateFrom, string dateTo, string timeFrom, string timeTo, string xBar, string rBar, int isUCLFixed, int isUCLRFixed, int isNextSeries, int isFixed)
        {

            InputData inputData = new InputData() { FromDate = dateFrom, FromTime = timeFrom, IsUCLFixed = isUCLFixed, IsUCLRFixed = isUCLRFixed,
                                                    IsFixed = isFixed,
                                                    IsUseNextTestSeries = isNextSeries,
                                                    LineID = lineID,
                                                    PartNoID = partNoID,
                                                    RBar = rBar,
                                                    FunctionalTestID = functionalTestID, 
                TestSeriesID = testPlanID, ToDate = dateTo, ToTime = timeTo, XBar = xBar };
            ChartBusiness chartBusiness = new ChartBusiness(inputData);
            ChartData chartData = chartBusiness.GetChartData();
            if (isFixed <= 0 && chartData.HasChart == false)//RereshMode
            {
                for (int i = 1; i < MetrixDataBusiness.GetFunctionalTestIDListByLine(inputData.LineID).Count; i++)
                {
                    inputData.IsUseNextTestSeries = 1;
                    inputData.PartNoID = chartData.OutputData.PartNoID;
                    inputData.FunctionalTestID = chartData.OutputData.FunctionalTestID;
                    inputData.TestSeriesID = chartData.OutputData.TestPlanID;
                    chartBusiness = new ChartBusiness(inputData);
                    chartData = chartBusiness.GetChartData();
                    chartData.LineHasNoValue = true;
                    if (chartData.HasChart)
                    {
                        chartData.LineHasNoValue = false;
                        break;
                    }
                }
            }
            JavaScriptSerializer js = new JavaScriptSerializer();
            string json = js.Serialize(chartData);
            return json;

        }

        [WebMethod]
        public static string GetFunctionalTestIDList(string testSeriesID)
        {
            List<TestPlanItem> functionalTestIDList = MetrixDataBusiness.GetFunctionalTestIDList(testSeriesID);
            JavaScriptSerializer js = new JavaScriptSerializer();
            string json = js.Serialize(functionalTestIDList);
            return json;
        }
    }
}
